package com.gdwhomework.ThreadHomework;


import java.util.Scanner;

/**
 * @author 关道伟
 * @version 1.0
 */
public class HomeWork01 {
    public static void main(String[] args) {
        T1 t1 = new T1();
        Thread thread1 = new Thread(t1);
        thread1.start();

        T2 t2 = new T2(t1);
        Thread thread2 = new Thread(t2);
        thread2.start();
    }
}

class T1 implements Runnable{
    private boolean loop = true;

    public boolean isLoop() {
        return loop;
    }

    public void setLoop(boolean loop) {
        this.loop = loop;
    }
    public  void getset(){
        setLoop(false);
    }


    @Override
    public void run() {
        for (int i = 1; i <= 100&&loop == true ; i++) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(i);
        }
    }
}

class T2 implements Runnable {
    private  T1 t1;

    public T2(T1 t1) {//在构造器中获取t1
        this.t1 = t1;
    }


    @Override
    public void run() {
        while(true){
            //接收输入
            System.out.println("请决定是否停止：（Q/q）");
            Scanner scanner = new Scanner(System.in);
            char key = scanner.next().toUpperCase().charAt(0);
            if(key == 'Q'){
                //已通知的方式结束线程t1
                t1.setLoop(false);
                //
                System.out.println("线程退出...");
                break;
            }
        }

    }
}